<?php 
Class PackageController extends Controller{
	private $emp_session ;
	private $can_session;
	public function __construct(){
		$this->calledClass(get_class($this));
		$this->emp_session = Session::get("employer");
		$this->can_session = Session::get("candidate");
	}
	public function DisplayFront(){
		$package= $this->loadModel("Package");
		$membership= $this->loadModel("Membership");
		$employer = $this->loadModel("Employer");
		$gallery = $this ->loadModel("Gallery");
		$emp_info = $employer->findObject(array("id="=>$this->emp_session["id"]));
		$this->set(array("employer"=>$emp_info));
		$this->set(array("emp_imgs"=>$gallery->findObject(array("id="=>$emp_info->img_id))));
		$this->set(array("listquery"=>$package->query(10)));
		$this->set(array("querypackage"=>$package->DisplayFront()));
		$this->set(array("user_id"=>$this->emp_session["id"]));
		$record = $membership->findAsObject(array("user_id="=>$this->emp_session["id"],"sub_type="=>"Employer"));
		if(is_object($record)){
			$this->set(array("point"=>$record->posting_point));
		}else{
			$this->set(array("point"=>0));
		}
		$this->set(array("layout"=>"package"));	
		$this->render(array("employer"=>"admin"));
	}
	public function InsertMembership(){
		if(isset($_POST['checkbox'])){
		$package= $this->loadModel("Package");
		$employer=$this->loadModel("Employer");
		$order= $this->loadModel("Order");
		/*query one row from database package that user check*/
		$findobjectpackage = $package->findObject(array("id="=>$this->get("checkbox")));
		$_SESSION['type'] = "Package";
		$order->InsertOrder($this->emp_session["id"],$findobjectpackage);
		 //Alert mail to admin to tell that have an employer buy package
		$recordEmp = $employer->findObject(array("id = "=>$this->emp_session["id"]));
		$recordPackage =$package->findObject(array("id="=>$this->get("checkbox")));
		$str = "Dear " .$recordEmp->firstname ." ".$recordEmp->lastname ."<br/><br/>";
		$str .= "You have bought a package:<br/>";
		$str .= "Package's Name : " .$recordPackage->title."<br/>";
		$str .= "Price : " .$recordPackage->price." $<br/>";
		$str .= "Point : " .$recordPackage->point."<br/>";
		$str .= "Valid Day : " .$recordPackage->valid_day." Days<br/>";
		ob_start();
		//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
		Import::util('class.phpmailer');
		//the true param means it will throw exceptions on errors, which we need to catch
		$mail = new PHPMailer(true);
		//telling the class to use SMTP
		$mail->IsSMTP();
		try {
			// SMTP server
			$mail->Host = "mail.atmos-techno.com";
			// enables SMTP debug information (for testing),  show debug : SMTPDebug  = 2,  Hide debug : SMTPDebug  = 1
			$mail->SMTPDebug = 1;
			// enable SMTP authentication
			$mail->SMTPAuth = true;
			// sets the SMTP server
			$mail->Host = "mail.atmos-techno.com";
			// set the SMTP port for the MY OWN DOMAIN server
			$mail->Port = 26;
			// SMTP account username
			$mail->Username = "demo@atmos-techno.com";
			// SMTP account password
			$mail->Password = "demo*123456";
			//$mail->AddAddress('phonpisey23@gmail.com','');
			$email_address = filter_input(INPUT_GET, $recordEmp->email, FILTER_VALIDATE_EMAIL);
			if (!$email_address) {
				$mail->SetFrom('demo@atmos-techno.com', 'HRCambodia');
				// $mail->AddAddress('info@atmos-techno.com', 'Atmosphere Technology Co.,Ltd');
				$mail->AddAddress( $recordEmp->email, $recordEmp->email);
				//$mail->AddAddress('phonpisey23@gmail.com','');
				$mail->AddReplyTo('demo@atmos-techno.com', 'sales@hrcambodia.com');
				$mail->Subject = "HRCambodia .com Alert Order Package";
				// optional - MsgHTML will create an alternate automatically
				$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
				$mail->MsgHTML("<h3>HRCambodia .com Alert Order Package</h3>" . $str . "<br/>");
				$mail->Send();
			} else {
				//echo 'Email is invalid...';
				break;
			}
		} catch (phpmailerException $e) {
			//Pretty error messages from PHPMailer
			echo $e->errorMessage();
		} catch (Exception $e) {
			//Boring error messages from anything else!
			echo $e->getMessage();
		} 
		//$this->set(array("queryorder"=>$order->DisplayOrder($this->get("order_id"))));
		$this->layout="admin_proceed_payment_display";
		$this->render();
		}else{
			//back to order credit again
			echo 1;
		}
	}
	/*
	 * Author Phon Pisey
	 * parameter: $addDay is for insert number of day that you 1 2 add
	 * culDate: for calculate date
	 */
	public function culDate($addDay){
		$date = date("Y-m-d H:i:s");
		$date = strtotime($date);
		$date = strtotime("+" . $addDay . " day" , $date);
		$totalpointexpire = date('Y-m-d H:i:s', $date);
		return $totalpointexpire;
	}
	public function UpdateStatus(){
		$order= $this->loadModel("Order");
		$order->UpdateStatus(array("status"=>"1"),array("id"=>$this->get("order_id")));
		$this->layout="front/display_proceed_payment";
		$this->render();
	}
	public function DisplayAdmin(){
		$package = $this->loadModel("Package");
		$currency = $this->loadModel("Currency");
		$query = $package->queryJoinTable();
		$this->set(array("getlist"=>$package->queryList()));
		$this->set(array("records"=>$package->DisplayAdmin($query,$this->get("page")?$this->get("page"):1,$this->get("perpage")?$this->get("perpage"):20,array("title"=>$this->get("title"),"price"=>$this->get("price"),"point"=>$this->get("point"),"valid_day"=>$this->get("validday")),array("id"),"ASC","AND")));
		$currencies = $currency->getCurrenciesAsArray();
		$this->set(array("currencies"=>$currencies));
		$this->set(array("current_menu"=>"credit"));
		$this->render("admin_display_package");
	}
	public function save(){
		$package = $this->loadModel("Package");
		$querycompare = $package->QueryOneRecord(array("title="=>$this->get("title")));
		if($querycompare == NULL){
			$package->save(array(
							"title"=>ucwords(trim($this->get("txtTitle"))),
							"price"=>trim($this->get("txtPrice")),
							"currency_id"=>trim($this->get("cboCurrency")),
							"point"=>trim($this->get("textPoint")),
							"valid_day"=>trim($this->get("valid_day")),
							"version"=>1,
							"created"=>date("y-m-d h-m-s")));
			//Redirect to main page
			$this->DisplayAdmin();
		}else{
			if($querycompare->title == ucwords(trim($this->get("title")))){
				echo 1;
			}else{
				$package->save(array(
								"title"=>ucwords(trim($this->get("txtTitle"))),
								"price"=>trim($this->get("txtPrice")),
								"currency_id"=>trim($this->get("cboCurrency")),
								"point"=>trim($this->get("textPoint")),
								"valid_day"=>trim($this->get("valid_day")),
								"version"=>1,
								"created"=>date("y-m-d h-m-s")));
				//Redirect to main page
				$this->DisplayAdmin();
			}	
		}	
	}
	public function DisplayFrontBuyPackage(){
		$package= $this->loadModel("Package");
		$order= $this->loadModel("Order");
		$this->set(array("querypackage"=>$package->DisplayFront()));
		$this->set(array("queryorder"=>$order->DisplayFront()));
		$this->set(array("layout"=>"buypackage"));
		$this->render("admin");
	}
	public function FrontChooseChoice(){
		$this->set(array("layout"=>"choosechoice"));
		$this->render("admin");
	}
	public function delete(){
		$package = $this->loadModel("Package");
		$checkedbox = $this->get("id");
		if($checkedbox){
			$package->delete(array($checkedbox),true);	
			$this->DisplayAdmin();
		}else {
			$this->DisplayAdmin();
		}
	}
	public function GetField(){
		$package = $this->loadModel("Package");
		$checkedbox = $this->get("id");	
		if($checkedbox){		
			$query = $package->queryJoinTable();
			$getfield= $package->GetFieldWithMultiTable($query,array("Packages.id="=>$checkedbox));
			$this->set(array("getfield"=>$getfield));
			$this->set(array("selectallcurrency"=>$package->query(10)));
			$this->render("admin_edit_package");
		}else {
			$this->DisplayAdmin();
		}
	}
	public function SaveEdit(){
		$package = $this->loadModel("Package");
		$checkedbox = $this->get("checkbox");
		$data = $package->findAsObject(array("id="=>$checkedbox));
		$version = $data->version + 1;
		$package->edit(array(
				"title"=>ucwords(trim($this->get("txtTitle"))),
				"price"=>ucwords(trim($this->get("txtPrice"))),
				"point"=>ucwords(trim($this->get("txtPoint"))),
				"currency_id"=>ucwords(trim($this->get("txtCurrency"))),
				"valid_day"=>ucwords(trim($this->get("txtValidDay"))),
				"version"=>$version,
				"modify"=>date("Y-m-d H:i:s")),array("id="=>$checkedbox));
		//Redirect to main page
		$currency = $this->loadModel("Currency");
		$query = $package->queryJoinTable();
		$this->set(array("getlist"=>$package->queryList()));
		$this->set(array("records"=>$package->DisplayAdmin($query,$this->get("page"),$this->get("perpage")?$this->get("perpage"):20,array("title"=>$this->get("title"),"price"=>$this->get("price"),"point"=>$this->get("point"),"valid_day"=>$this->get("validday")),array("id"),"ASC","AND")));
		$currencies = $currency->getCurrenciesAsArray();
		$this->set(array("currencies"=>$currencies));
		$this->set(array("current_menu"=>"credit"));
		$this->render(array("admin"=>"credit"));
	}
	public function AddNew(){
			$package = $this->loadModel("Package");
			$this->set(array("selectallcurrency"=>$package->query(10)));
			$this->set(array("layout"=>"addnew"));
			$this->render("admin");
		
	}
}
?>
